package cb.com.etcdemo;

import android.Manifest;
import android.app.Activity;
import android.app.Application;
import android.app.Service;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.os.PowerManager;
import android.os.StrictMode;
import android.os.Vibrator;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

//import com.baidu.mapapi.SDKInitializer;


/**
 * 应用Application类
 *
 * @author 崔斌
 * @data
 */

public class BaseApplication extends Application {

    private static BaseApplication mContext;

    public static final String[] authBaseArr = {Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_FINE_LOCATION};
    public static String authinfo = null;
    public static final String[] authComArr = {Manifest.permission.READ_PHONE_STATE};

    public Vibrator mVibrator;
    // 单例模式中获取唯一的ExitApplication 实例
    public static BaseApplication getInstance() {
        return mContext;

    }
    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        /***
         * 初始化定位sdk，建议在Application中创建
         */

        int powerLevel = PowerManager.FULL_WAKE_LOCK;
        PowerManager pm = (PowerManager) this
                .getSystemService(mContext.POWER_SERVICE);
//        AppConstant.powerlock = pm.newWakeLock(powerLevel, "camera");
        getokhttp();
        initComUtil();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }
        /***
         * 初始化定位sdk，建议在Application中创建
         */
        mVibrator =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
    }

    // 静态方法返回该类的实例
    public void initComUtil() {


    }


    public void getokhttp() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(30000L, TimeUnit.MILLISECONDS)
                .readTimeout(30000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();

        OkHttpUtils.initClient(okHttpClient);
    }


    private List<Activity> mList = new LinkedList<Activity>();

    // add Activity
    public void addActivity(Activity activity) {
        mList.add(activity);
    }

    public void exit() {
        try {
            for (Activity activity : mList) {
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //会黑屏
//            System.exit(0);
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
    }





    private String getSdcardDir() {
        if (Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
            return Environment.getExternalStorageDirectory().toString();
        }
        return null;
    }

    /**
     * 检查权限
     *
     * @return
     */
    public boolean hasBasePhoneAuth() {
        PackageManager pm = this.getPackageManager();
        for (String auth : authBaseArr) {
            if (pm.checkPermission(auth, this.getPackageName()) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    /**
     * 权限初始化完成是否
     *
     * @return
     */
    public boolean hasCompletePhoneAuth() {
        PackageManager pm = this.getPackageManager();
        for (String auth : authComArr) {
            if (pm.checkPermission(auth, this.getPackageName()) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }


}
